package com.aier.user.action;

import javax.annotation.Resource;

import net.eicp.ghostboy.futil.MD5Util;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.user.common.UserPhoneUtil;
import com.aier.user.entity.User;
import com.aier.user.service.IUserService;

@Scope("prototype")
@Component("userRetrievePwdAction")
public class UserRetrievePwdAction extends UserBaseAction{

    /**
     * 找回密码页面
     */
    private static final long serialVersionUID = 1L;

    @Resource(name = "userService")
    private IUserService userService;    
    
    private int s;                //成功:1 ,失败:0
    private String error_info;    //错误提示信息
    
    private String account;        //账号(手机号)
    private String pwd;            //密码
    private String message;        //手机验证码
    private String oldpwd;         //旧密码
    
    
    @Override
    public String execute(){
        
        return SUCCESS;
    }
    
    //检查手机号是否可用
    public String checkPhone(){
        s = 1;
        if(UserPhoneUtil.isMobileNO(account)){
            if(userService.searchUserByCondition(account)==null){
                s = 0;
                error_info = "该手机号没有注册";
            }
        }else{
            s = 0;
            error_info = "手机号格式有误";
        }
        return SUCCESS;
    } 
    
    public String checkCode(){
        if(userService.checkPhoneMessage(account, message)==0){
            s = 0;
            error_info = "短信验证码有误";
        }else{
            s = 1;
        }
        return SUCCESS;
    }
    
    public String changeUserPwd(){
        s = userService.updateUserPwd(account, MD5Util.getMD5(pwd));
        return SUCCESS;
    }
    
    /**
     * 忘记密码
     * @return
     */
    public String forgetPwd(){
    	User u=userService.searchUserByCondition(account);
    	s = userService.updateUserPwd(account, MD5Util.getMD5(pwd));
    	return SUCCESS;
    }
    
    /**
     * 修改密码
     * @return
     */
    public String changeUserPwdApp(){
    	User u=userService.searchUserByCondition(account);
    	if(MD5Util.getMD5(oldpwd).equals(u.getPwd())==false){
    		error_info = "原密码错误";
    		s=0;
    	}
        s = userService.updateUserPwd(account, MD5Util.getMD5(pwd));
        return SUCCESS;
    }

    public int getS() {
        return s;
    }

    public String getError_info() {
        return error_info;
    }

    public void setUserService(IUserService userService) {
        this.userService = userService;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getAccount() {
        return account;
    }

    public String getMessage() {
        return message;
    }

	public void setOldpwd(String oldpwd) {
		this.oldpwd = oldpwd;
	}
}
